// pages/tixianmingxi/tixianmingxi.js
const app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gettixianinfourl:app.data.url+'/mapi/newfenxiao/gettixianinfo',
    title:[
      {
        name:'全部',
      },
      {
        name: '未打款',
      },
      {
        name: '已打款',
      },
      {
        name: '驳回',
      }
    ],
    indexs:0,
    shuju:[
      [
      ],
      [
      ],
      [
      ],
      [
      ]
    ],
    allpage:1,
    weipage:1,
    yipage:1,
    bopage:1,
  },

  aa: function (e) {
    var indexs = e.currentTarget.dataset.index;
    console.log(indexs);
    this.setData({indexs:indexs});
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllData();
    this.getWeiDaKuan();
    this.yiDakuan();
    this.bohuiData();
  },
  fuzhi:function(e){
    var str = e.currentTarget.dataset.str;
    wx.setClipboardData({
      data: str,
      success:function(res){
        console.log(res);
      }
    })
    // console.log(str);
  },
  //获取全部的数据
  getAllData(){
    var that = this;
    var user = wx.getStorageSync('user');
    var page = that.data.allpage;
    if(page==0){
      wx.showToast({
        title: '没有更多数据了！',
        icon:'none'
      });
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.gettixianinfourl,
      data:{token:user.token,page:page},
      method:'post',
      success:function(res){
        var data = res.data;
        if(data.code==200){
          var shuju = that.data.shuju;
          var newshuju = shuju[0];
          var array = data.data.data;
          console.log(array);
          that.setData({ allpage: page+1 });
          if(array.length<10){
            that.setData({allpage:0});
          }
          ea.each(array,function(i,v){
            newshuju.push(v);
          })
          shuju[0] = newshuju;
          that.setData({shuju:shuju});
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }
        wx.hideLoading();
      }
    })
  },
  //获取未打款的项目
  getWeiDaKuan:function(){
    var that = this;
    var user = wx.getStorageSync('user');
    var page = that.data.weipage;
    if (page == 0) {
      wx.showToast({
        title: '没有更多数据了！',
        icon: 'none'
      });
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.gettixianinfourl,
      data: { token: user.token, page: page,type:1 },
      method: 'post',
      success: function (res) {
        var data = res.data;
        if (data.code == 200) {
          var shuju = that.data.shuju;
          var newshuju = shuju[1];
          var array = data.data.data;
          console.log(array);
          that.setData({ weipage: page + 1 });
          if (array.length < 10) {
            that.setData({ weipage: 0 });
          }
          ea.each(array, function (i, v) {
            newshuju.push(v);
          })
          shuju[1] = newshuju;
          that.setData({ shuju: shuju });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
        wx.hideLoading();
      }
    })
  },
//获取已打款
yiDakuan:function(){
  var that = this;
  var user = wx.getStorageSync('user');
  var page = that.data.yipage;
  if (page == 0) {
    wx.showToast({
      title: '没有更多数据了！',
      icon: 'none'
    });
    return;
  }
  wx.showLoading({
    title: '加载中',
  })
  wx.request({
    url: that.data.gettixianinfourl,
    data: { token: user.token, page: page, type: 2 },
    method: 'post',
    success: function (res) {
      var data = res.data;
      if (data.code == 200) {
        var shuju = that.data.shuju;
        var newshuju = shuju[2];
        var array = data.data.data;
        console.log(array);
        that.setData({ yipage: page + 1 });
        if (array.length < 10) {
          that.setData({ yipage: 0 });
        }
        ea.each(array, function (i, v) {
          newshuju.push(v);
        })
        shuju[2] = newshuju;
        that.setData({ shuju: shuju });
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
      wx.hideLoading();
    }
  })
},
//驳回的数据
bohuiData:function(){
  var that = this;
  var user = wx.getStorageSync('user');
  var page = that.data.bopage;
  if (page == 0) {
    wx.showToast({
      title: '没有更多数据了！',
      icon: 'none'
    });
    return;
  }
  wx.showLoading({
    title: '加载中',
  })
  wx.request({
    url: that.data.gettixianinfourl,
    data: { token: user.token, page: page, type: 3 },
    method: 'post',
    success: function (res) {
      var data = res.data;
      if (data.code == 200) {
        var shuju = that.data.shuju;
        var newshuju = shuju[3];
        var array = data.data.data;
        console.log(array);
        that.setData({ bopage: page + 1 });
        if (array.length < 10) {
          that.setData({ bopage: 0 });
        }
        ea.each(array, function (i, v) {
          newshuju.push(v);
        })
        shuju[3] = newshuju;
        that.setData({ shuju: shuju });
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
      wx.hideLoading();
    }
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      shuju: [[], [], [], []], allpage: 1,
      weipage: 1,
      yipage: 1,
      bopage: 1,
      indexs:0});
      this.onLoad();
    wx.stopPullDownRefresh({
      success: function () {
        console.log('返回了')
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var indexs = this.data.indexs;
      if(indexs==0){
        this.getAllData();
      }else if(indexs==1){
        this.getWeiDaKuan();
      }else if(indexs==2){
        this.yiDakuan();
      }else if(indexs==3){
        this.bohuiData();
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})